#ifndef CONTROL_EVENT_DISPATCHER_H_INCLUDED
#define CONTROL_EVENT_DISPATCHER_H_INCLUDED

#include <control_dispatcher.h>
#include <control_terminal.h>

#include <sys/signalfd.h>
#include <cerrno>
#include <unistd.h>

#include <utils/event_counter.h>
#include <utils/system_exception.h>

class ControlTerminal::EventDispatcher
    : public ControlDispatcher
{
public:
    explicit EventDispatcher(EventDispatcher * & dispRef)
        : dispRef_(dispRef)
    {
        dispRef_ = this;
    }

    ~EventDispatcher() throw()
    {
        dispRef_ = 0;
    }

    int fd() const
    {
        return counter_.fd();
    }

    /*virtual*/
    bool dispatch(ControlTerminal & term)
    {
        return term.onEvent(counter_);
    }

    void notify(uint64_t x = 1)
    {
        counter_ += x;
    }

private:
    EventDispatcher * &  dispRef_;
    utils::event_counter counter_;

};

#endif // CONTROL_EVENT_DISPATCHER_H_INCLUDED
